﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using ThirdServices.AlertModel.Factory.BaseInterface;
using ThirdServices.AlertModel.Factory.Entities.DTO;

namespace ThirdServices.AlertModel.Wanderground
{
    public class WandergroundGlobalAlert : IAlert
    {
        #region IAlert Members

        public double Lat{get;set;}
        public double Lon { get; set; }
        public double Radius { get; set; }

        public List<BaseAlert> CheckAlert()
        {
            var listWGGAS = new List<WandergroundGlobalAlertStructure>();
            WandergroundGlobalAlertStructure wggas;
            Console.WriteLine("Global");
            string url = AlertsLinks.WunderAlertLink(Lat, Lon);
            XDocument xDoc = XDocument.Load(url);
            if (xDoc.Descendants("AlertItem").Count() == 0 )
            {
                return new List<BaseAlert>();
            }
            XElement xElement = xDoc.Descendants("AlertItem").First();
            foreach (XElement xEl in xDoc.Descendants("AlertItem"))
            {
                wggas = new WandergroundGlobalAlertStructure();
                wggas.type = xEl.Element("type").Value;
                wggas.description = xEl.Element("description").Value;
                wggas.date = xEl.Element("date").Value;
                wggas.message = xEl.Element("message").Value;
                wggas.phenomena = xEl.Element("phenomena").Value;
                wggas.significance = xEl.Element("significance").Value;
                listWGGAS.Add(wggas);
            }
            var baseAlerts = new List<BaseAlert>();
            foreach (WandergroundGlobalAlertStructure WanderGlobalAlertStruct in listWGGAS)
            {
                BaseAlert tmpBaseAlert = new BaseAlert();
                NotificationDescription description = new NotificationDescription
                                                          {
                                                              alert_type = AlertTypes.GlobalAlert,
                                                              notification = WanderGlobalAlertStruct.description + "\n" + WanderGlobalAlertStruct.date + "\n" +
                                                                 WanderGlobalAlertStruct.message + "\n" + WanderGlobalAlertStruct.phenomena + "\n" +
                                                                 WanderGlobalAlertStruct.significance
                                                          };
                tmpBaseAlert.NotificationDescription = description;
                tmpBaseAlert.NotificationDescription = description;
                baseAlerts.Add(tmpBaseAlert);
            }

            return baseAlerts;
        }

        #endregion

        public void GetAlert()
        {
        }
    }
}
